﻿<?xml version="1.0" encoding="utf-8"?>
<!--
        作者：          liuleyi
        更新时间：      20170227
        用途：          生成存储过程默认模板
-->
<xsl:stylesheet version="2.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	xmlns:fn="http://www.w3.org/2005/xpath-functions">
	<xsl:output method="text" indent="no" encoding="utf-8" byte-order-mark="no"/>  
	<xsl:template match="/">

	<xsl:variable name="StringTypeDefine" select="document('../model/0101_StringTypeDefine.xml')"/>
	<xsl:variable name="EnumTypeDefine" select="document('../model/0102_EnumTypeDefine.xml')"/>
	<xsl:variable name="BooleanTypeDefine" select="document('../model/0103_BooleanTypeDefine.xml')"/>
	<xsl:variable name="DateTypeDefine" select="document('../model/0104_DateTypeDefine.xml')"/>
	<xsl:variable name="NumberTypeDefine" select="document('../model/0105_NumberTypeDefine.xml')"/>
	<xsl:variable name="IntegerTypeDefine" select="document('../model/0106_IntegerTypeDefine.xml')"/>

<xsl:for-each select="FunNoConfig/FunNo">
<xsl:variable name="fileName" select="concat('./generate/php/Admin/Func',@code,'.class.php')"/>
<xsl:result-document encoding="utf-8" method="text" href="{$fileName}">&lt;?php
/*
*<xsl:value-of select="@label"/>
*/
namespace Admin\Func;

class Func<xsl:value-of select="@code"/> {
    var  $db;
    var $rst;
    var $errmsg;
<xsl:for-each select="Request">
<xsl:for-each select="Column">    var  $<xsl:value-of select="@name"/>;
</xsl:for-each>
</xsl:for-each>
    public function __construct(<xsl:for-each select="Request"><xsl:for-each select="Column">$<xsl:value-of select="@name"/><xsl:if test="position()!=last()">,</xsl:if></xsl:for-each>
</xsl:for-each>) {
        $this->db  = new \Think\Model();
        $this->errmsg = '';
<xsl:for-each select="Request">
<xsl:for-each select="Column">        $this-><xsl:value-of select="@name"/> = addslashes($<xsl:value-of select="@name"/>);
</xsl:for-each>
</xsl:for-each>
    }
    /*
    *获取错误信息
    */
    public function getErrMsg() {
        return $this->errmsg;
    }
    /*
    *验证
    */
    private function validate(){
        return true;
    }
    /*
    *执行
    */
    public function execute(){
        $vflag = true;
        $OperID = I('OperID');
        if($this->validate()){

            $sql = &lt;&lt;&lt;EOD
<xsl:choose><xsl:when test="@dml='INSERT'">INSERT INTO <xsl:value-of select="@table"/>(
<xsl:for-each select="Request">
<xsl:for-each select="Column">        <xsl:value-of select="@name"/>,
</xsl:for-each>
</xsl:for-each>
        OperID,
        OperDate
        )values(
<xsl:for-each select="Request">
<xsl:for-each select="Column">        '$this-><xsl:value-of select="@name"/>',
</xsl:for-each>
</xsl:for-each>
        '$OperID',
        now()
)
</xsl:when>
<xsl:otherwise>
<xsl:choose><xsl:when test="@dml='UPDATE'">UPDATE <xsl:value-of select="@table"/> t
SET <xsl:for-each select="Request">
<xsl:for-each select="Column">T.<xsl:value-of select="@name"/> = '$this-><xsl:value-of select="@name"/>',
</xsl:for-each>
</xsl:for-each>
T.OperID = '$OperID',
T.OperDate = now()
WHERE T.<xsl:value-of select="@pk"/> = '$this-><xsl:value-of select="@pk"/>'
</xsl:when>
<xsl:otherwise>
<xsl:choose><xsl:when test="@dml='DELETE'">DELETE FROM <xsl:value-of select="@table"/>
WHERE 1=1 <xsl:for-each select="Request">
<xsl:for-each select="Column"> AND <xsl:value-of select="@name"/> = '$this-><xsl:value-of select="@name"/>' 
</xsl:for-each>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>SELECT * FROM <xsl:value-of select="@table"/> T WHERE 1=1 <xsl:for-each select="Request">
<xsl:for-each select="Column"> AND T.<xsl:value-of select="@name"/> = '$this-><xsl:value-of select="@name"/>' 
</xsl:for-each>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise></xsl:choose>
EOD;

            //$this->rst = $this->db->query($sql);
            $this->rst = $this->db->execute($sql);
            if ($this->rst === false){
                $this->errmsg = $this->db->getDbError();
                $vflag = false;
            }else{
                $vflag = true;  
            }
        }else{
            $vflag = false;
        }
    return  $vflag;
    }
    public function getRst(){
        return $this->rst;
    }
}
</xsl:result-document>
</xsl:for-each>
</xsl:template>  
</xsl:stylesheet>